package com.apes.scm.settle.settle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.invoice.model.InvoiceItem;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SettleSimpleItem.CONST_TABLE_NAME)
public class SettleSimpleItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_settle_simple_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "结算商品")
    @Comment("结算单明细(简)号")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "settle_simple_id", referencedColumnName = "id")
    @Comment("结算简单号")
    private SettleSimple settleSimple;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="product_id", referencedColumnName="id", nullable = false)
    @Comment("商品编码")
    private Product product;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom;

    @Column(name = "origin_business_type", length = 32, nullable = true)
    @Comment("源单类型")
    private String originBusinessType;

    @Column(name = "origin", length = 30, nullable = true)
    @Comment("源单号")
    private String origin;

    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("单价")
    private double priceUnit = 0;

    @Column(name = "pending_qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("待结算数量")
    private double pendingQty = 0;

    @Column(name = "actual_qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("结算数量")
    private double actualQty = 0;

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("结算金额")
    private double amount = 0;

    @Column(name = "free_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("免收金额")
    private double freeAmount = 0;

    @Column(name = "repletion_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("实结金额")
    private double repletionAmount = 0;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "occurrence_date")
    @Comment("业务发生日期")
    private Date occurrenceDate;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "invoice_id", referencedColumnName = "id")
    @Comment("账单明细编码")
    private InvoiceItem invoiceItem;

    @Column(name = "source", length = 32)
    @Comment("来源单号")
    private String source;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "car_license", length = 64)
    @Comment("车牌号码")
    private String carLicense;

    @Column(name = "amt_tax", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("税金")
    private double amtTax = 0;

    @Column(name = "note", length = 250)
    private String note; //备注

    @Column(name = "invoiced_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("已开票金额")
    private double invoicedAmount = 0;

    @Column(name = "invoice_number", length = 256)
    @Comment("发票号码")
    private String invoiceNumber;


    @Transient
    private boolean selected;

    @Transient
    private Dept dept;

    public Dept getDept() {
        if(Objects.isNull(invoiceItem))return this.dept;
        return invoiceItem.getInvoice() != null ? invoiceItem.getInvoice().getDept() : null;
    }

    public boolean getSelected() {
        return this.selected;
    }
}
